package com.sgb.kjwl.view.ui.adapter.home;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.View;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemLeaseSearchResultBinding;
import com.sgb.kjwl.databinding.ItemMachineListBinding;
import com.sgb.kjwl.model.entity.LeaseSearchResultEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

/**
 * 租赁-搜索结果列表itme
 */
public class LeaseSearchResultAdapter extends BaseRecycleAdapter<LeaseSearchResultEntity> {

    private double rentalPriceInt;

    private Context context;

    public LeaseSearchResultAdapter(Context context, List<LeaseSearchResultEntity> datas) {
        super(context, R.layout.item_lease_search_result, datas);
        this.context = context;
    }

    @Override
    public void convert(ItemViewHolder holder, final LeaseSearchResultEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ItemLeaseSearchResultBinding binding = (ItemLeaseSearchResultBinding) holder.getmBinding();
        String rentalPrice = entity.getPrice();
        // BigDecimal 类的价格
        try {
            rentalPriceInt = Double.parseDouble(rentalPrice);
            if (rentalPriceInt <= 0) {
                binding.priceView.setText("面议");
            } else {
                binding.priceView.setText(entity.getPrice() + entity.getPriceUnit());
            }
        } catch (Exception e) {
            binding.priceView.setText("面议");
        }

        if ("1".equals(entity.getIsSelf())) {//是否自营，1为自营
            SpannableString spannableString = new SpannableString("  " + entity.getTitle());
            Drawable d = context.getResources().getDrawable(R.mipmap.ic_lease_isself);
            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            spannableString.setSpan(new ImageSpan(d), 0, 1, ImageSpan.ALIGN_BASELINE);
            binding.titleView.setText(spannableString);
        } else {
            binding.titleView.setText(entity.getTitle());
        }

        binding.tvArea.setText(entity.getCity() + entity.getArea());
        if (!TextUtils.isEmpty(entity.getDistance())) {
            binding.tvDistance.setVisibility(View.VISIBLE);
            binding.tvDistance.setText(entity.getDistance());
        } else {
            binding.tvDistance.setVisibility(View.GONE);
        }
//        try {
//            binding.tvTime.setText(DateUtils.stampToDay(entity.getUpdatetime()));
//        } catch (Exception e) {
//
//        }
    }

}
